/*
 * @Author: jqm123 2289662078@qq.com
 * @Date: 2025-07-25 17:10:32
 * @LastEditors: jqm123 2289662078@qq.com
 * @LastEditTime: 2025-08-13 08:44:49
 * @FilePath: /bmxt/src/router/index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2025-06-12 14:59:08
 * @LastEditors: jqm123 2289662078@qq.com
 * @LastEditTime: 2025-08-12 14:06:07
 * @FilePath: /mobile_demo_v3/src/router/index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import {createRouter, createWebHashHistory} from "vue-router";
import Layout from "@/layout/tabbar.vue";
import {useUserStore} from "@/stores";

const routes = [
    {
        path: "/",
        name: "Layout",
        component: Layout,
        redirect: "/home",
        children: [
            {
                path: "/home",
                name: "home",
                component: () => import("@/views/home.vue"),
                meta: {
                    title: "home",
                },
            },
            {
                path: "/my",
                name: "my",
                component: () => import("@/views/my/my.vue"),
                meta: {
                    title: "my",
                },
            },
        ],
    },
    {
        path: "/detail",
        name: "detail",
        component: () => import("@/views/enroll/detail.vue"),
        meta: {
            title: "detail",
        },
    },
    {
        path: "/enroll",
        name: "enroll",
        component: () => import("@/views/enroll/enroll.vue"),
        meta: {
            title: "enroll",
        },
    },
    {
        path: "/my_enroll_detail",
        name: "my_enroll_detail",
        component: () => import("@/views/enroll/my_enroll_list.vue"),
        meta: {
            title: "my_enroll_detail",
        },
    },
    {
        path: "/contact",
        name: "contact",
        component: () => import("@/views/my/contact.vue"),
        meta: {
            title: "contact",
        },
    },
    {
        path: "/404",
        name: "404",
        component: () => import("@/views/404.vue"),
        meta: {
            title: "404",
        },
    },
    {
        path: "/enroll_zba",
        name: "enroll_zba",
        component: () => import("@/views/enroll/enroll_zba.vue"),
        meta: {
            title: "浙ba第二场地报名",
        },
    },
    {
        path: "/enroll_szct",
        name: "enroll_szct",
        component: () => import("@/views/enroll/enroll_szct.vue"),
        meta: {
            title: "失重餐厅报名",
        },
    },
    {
        path: "/enroll_zcjn",
        name: "enroll_zcjn",
        component: () => import("@/views/enroll/enroll_zcjn.vue"),
        meta: {
            title: "职称技能报名",
        },
    },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

router.beforeEach(async (to, from, next) => {
    const userStore = useUserStore();
    // userStore.login()
    // next()
    // 1. 定义无需认证的白名单路径
    const allowList = ["/404"]; // 添加登录页等
    if (allowList.includes(to.path)) {
        next();
    }
    if (!userStore.token) {
        const res = await userStore.login();
        if (!userStore.token) {
            next({
                path: "/404",
            });
        } else {
            next();
        }
    }
    next();
});

export default router;
